package com.lj.demo.ucenterService.controller;


import com.lj.demo.commonutils.JwtUtils;
import com.lj.demo.commonutils.R;
import com.lj.demo.commonutils.daoVo.UcenterMemberVo;
import com.lj.demo.servicebase.exception.AException;
import com.lj.demo.ucenterService.entity.UcenterMember;
import com.lj.demo.ucenterService.entity.vo.RegisterVo;
import com.lj.demo.ucenterService.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author liujiang
 * @since 2020-12-23
 */
@Api(description = "登录注册接口")
@RestController
@RequestMapping("/ucenterService/ucenterMember")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody UcenterMember ucenterMember){
       String token = ucenterMemberService.login(ucenterMember);
       return R.ok().data("token",token);
    }
    @ApiOperation(value = "用户注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }


    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            UcenterMember ucenterMember = ucenterMemberService.getById(memberId);
            return R.ok().data("item", ucenterMember);
    }

    @ApiOperation(value = "根据token字符串获取用户信息")
    @PostMapping("getInfo/{id}")
    public UcenterMember getInfo(@PathVariable String id){
        UcenterMember member = ucenterMemberService.getById(id);
        return member;
    }

    @ApiOperation("根据token字符串id获取用户信息")
    @PostMapping("getUc/{id}")
    public UcenterMemberVo getInfoUc(@PathVariable String id){
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        UcenterMemberVo ucenterMemberVo = new UcenterMemberVo();
        BeanUtils.copyProperties(ucenterMember,ucenterMemberVo);
        return ucenterMemberVo;
    }

    @ApiOperation("统计某一天的注册人数")
    @GetMapping(value = "countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count = ucenterMemberService.countRegister(day);
        return R.ok().data("count",count);
    }

}


